package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.PlayBean;
import com.kaiyidiannao.model.req.base.BaseCommand;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *   玩乐住, 美食街,石子街 公用的请求数据了类-列表 异步请求
 */
public class GetPlayLIst extends BaseCommand {

    private int catid; //当前分类编号
    private int page =  DataCenter.LISTVIEW_DEFAULT_PAGE; //当前页码
    private int  order = 0;//排序方式
    private String TAG = "GetPlayLIst";
    private String geotable_id = "59254";
    private String longitude = "";
    private String ak = "";
    private String location;
    private String radius;
    private String sortby;
    private String mid;
    public GetPlayLIst(int catid) {
        this.catid = catid;
    }

    public GetPlayLIst(int catid, int page) {
        this.catid = catid;
        this.page = page;
    }

    public GetPlayLIst(int catid,String ak,String geotable_id, String location,String radius,String page,String sortby) {
        this.catid = catid;
        this.geotable_id = geotable_id;
        this.location = location;
        this.radius = radius;
        this.ak = ak;
        this.page = Integer.parseInt(page);
        this.sortby = sortby;
    }

    public GetPlayLIst(int catid,String ak,String geotable_id, String location,String radius,String page) {
        this.catid = catid;
        this.geotable_id = geotable_id;
        this.location = location;
        this.radius = radius;
        this.ak = ak;
        this.page = Integer.parseInt(page);
    }

    public GetPlayLIst(String mid,String ak,String geotable_id, String location,String radius,String page) {
        this.mid = mid;
        this.geotable_id = geotable_id;
        this.location = location;
        this.radius = radius;
        this.ak = ak;
        this.page = Integer.parseInt(page);
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_BAIDU_ADDRESS;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        if(catid!=0){
            paramters.put("filter", "catid:"+catid+","+catid);
        }else {
            paramters.put("filter", "mid:"+mid+","+mid);
        }
        paramters.put("geotable_id",geotable_id);
        paramters.put("location",location);
        paramters.put("radius",radius);
        paramters.put("ak",ak);
        paramters.put("page_index",page+"");
        if(!"".equals(sortby)&&null!=sortby){
            paramters.put("sortby",sortby);
        }
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetPlayLIst.yes()");

        try {
            Log.d(TAG, result.object.toString());

            JSONObject jsonObject = new JSONObject(result.object.toString());
            JSONArray jsonArray = jsonObject.optJSONArray("contents");

            ArrayList<PlayBean> targetList = DataCenter.playBeanList;

            if (page == DataCenter.LISTVIEW_BAIDU_DEFAULT_PAGE) {
                targetList.clear();
            }
            Log.d("GETPLAYLIST",jsonArray.length()+"");
//            for(int i = 0; i < jsonArray.length(); ++i){ // i从1开始，屏蔽提示信息
            for (int i = 0;i<jsonArray.length();i++){
                JSONObject o = (JSONObject) jsonArray.get(i);
                PlayBean bean = new PlayBean();
                bean.setSize(jsonArray.length()+"");
                bean.setThumb(o.getString("thumb"));
                if (null != o.get("star")) {
                    bean.setStar(o.getString("star"));
                }
                bean.setShopLongitude(o.optJSONArray("location").get(0)+"");
                bean.setShopLatitude(o.optJSONArray("location").get(1)+"");
                bean.setTitle(o.getString("title"));
                bean.setAddress(o.getString("address"));
                bean.setDistance(o.getString("distance"));
                bean.setWeight(o.getString("weight"));
                bean.setItemid(o.getString("itemid"));
                bean.setCatid(o.getString("catid"));
                targetList.add(bean);
            }
//            }
            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_PLAY_LIST_SUCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetPlayLIst.no()");

//        DataCenter.playBeanList.clear();
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_PLAY_LIST_FAILED);

    }
}
